package com.net.util;

import java.util.Map;

import com.net.model.UserFlow;
import com.net.model.UserFlowStatData;
import com.net.model.WebsiteFlow;
import com.net.model.WebsiteFlowStatData;

public class FlowStatUtil {
	public static void flowStat(String src_ip, String dst_ip, int len){
		WebsiteFlowStatData websiteFlowStatData = SpringUtils.getBean("website_flow_data");
		UserFlowStatData userFlowStatData = SpringUtils.getBean("user_flow_data");
		
		Map<String, WebsiteFlow> websiteData = websiteFlowStatData.getData();
		Map<String, UserFlow> userData = userFlowStatData.getData();
		
		//先通过源IP测试，看是否是监控中的网站
		UserFlow userFlow = userData.get(src_ip);
		WebsiteFlow websiteFlow = websiteData.get(dst_ip);
		String type = null;
		int cursor = 0;
		if(userFlow != null && websiteFlow != null){//源IP是我们要监控的用户
			 type = websiteFlow.getType();
			cursor = websiteFlow.getCursor();
			//统计用户流量
			
				int userCursor = userFlow.getCursor();
				Double[] userFlowPerMinute = userFlow.getFlowPerMinute();
				if(userFlowPerMinute[userCursor] == null){
					userFlowPerMinute[userCursor] = 0.0;
				}
				userFlowPerMinute[userCursor] = userFlowPerMinute[userCursor] + len;
				
				Map<String, Double> flowPerType = userFlow.getFlowPerType();
				Double tempFlowPerType = flowPerType.get(type);
				if(tempFlowPerType == null){
					tempFlowPerType = 0.0;
				}
				tempFlowPerType = tempFlowPerType + len;
				flowPerType.put(type, tempFlowPerType);
				
				userFlow.setFlowPerMinute(userFlowPerMinute);
				userFlow.setFlowPerType(flowPerType);
				userData.put(src_ip, userFlow);
				userFlowStatData.setData(userData);
			
			//统计网站流量
			Double[] flowPerMinute = websiteFlow.getFlowPerMinute();
			if(flowPerMinute[cursor] == null){
				flowPerMinute[cursor] = 0.0;
			}
			flowPerMinute[cursor] = flowPerMinute[cursor] + len;
			websiteFlow.setFlowPerMinute(flowPerMinute);
			websiteData.put(dst_ip, websiteFlow);
			websiteFlowStatData.setData(websiteData);
		}
		
		
		
		userFlow = userData.get(dst_ip);
		websiteFlow = websiteData.get(src_ip);
		type = null;
		cursor = 0;
		if(userFlow != null && websiteFlow != null){//源IP是我们要监控的用户
			 type = websiteFlow.getType();
			cursor = websiteFlow.getCursor();
			//统计用户流量
			
				int userCursor = userFlow.getCursor();
				Double[] userFlowPerMinute = userFlow.getFlowPerMinute();
				if(userFlowPerMinute[userCursor] == null){
					userFlowPerMinute[userCursor] = 0.0;
				}
				userFlowPerMinute[userCursor] = userFlowPerMinute[userCursor] + len;
				
				Map<String, Double> flowPerType = userFlow.getFlowPerType();
				Double tempFlowPerType = flowPerType.get(type);
				if(tempFlowPerType == null){
					tempFlowPerType = 0.0;
				}
				tempFlowPerType = tempFlowPerType + len;
				flowPerType.put(type, tempFlowPerType);
				
				userFlow.setFlowPerMinute(userFlowPerMinute);
				userFlow.setFlowPerType(flowPerType);
				userData.put(dst_ip, userFlow);
				userFlowStatData.setData(userData);
			
			//统计网站流量
			Double[] flowPerMinute = websiteFlow.getFlowPerMinute();
			if(flowPerMinute[cursor] == null){
				flowPerMinute[cursor] = 0.0;
			}
			flowPerMinute[cursor] = flowPerMinute[cursor] + len;
			websiteFlow.setFlowPerMinute(flowPerMinute);
			websiteData.put(src_ip, websiteFlow);
			websiteFlowStatData.setData(websiteData);
		}
	
			
		
	} 
}
